package com.woniuxy.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.user.entity.Role;
import com.woniuxy.user.exception.PreventDeleteException;
import com.woniuxy.user.mapper.RoleMapper;
import com.woniuxy.user.service.RoleService;
import com.woniuxy.user.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public ResPage<Role> getRoleList(RoleVo roleVo) {
        PageHelper.startPage(roleVo.getPage(),roleVo.getSize());
        List<Role> list = roleMapper.getRoleList(roleVo);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new ResPage<>(pageInfo);
    }

    @Override
    public Boolean checkName(String name) {
        return roleMapper.checkName(name) == 0;
    }

    @Override
    public Boolean addRole(Role role) {
        return roleMapper.addRole(role) == 1;
    }

    @Override
    public Boolean editRole(Role role) {
        return roleMapper.editRole(role) == 1;
    }

    @Override
    public Boolean deleteRole(Integer id) {
        //判断该角色是否有成员使用，如果有，则不能删除
        if (roleMapper.checkUserRole(id) > 0 ) {
            throw new PreventDeleteException("删除失败,该角色有用户正在使用");
        }
        //判断该角色下是否有权限，如果有，则不能删除
        if (roleMapper.checkRolePerms(id) > 0) {
            throw new PreventDeleteException("删除失败，该角色下有权限");
        }
        return roleMapper.deleteRole(id) == 1;
    }

    @Override
    public List<Role> getRoleListForEmployee() {
        return roleMapper.getRoleListForEmployee();
    }
}
